package hhm.android.library.datePicker;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.Date;

import hhm.android.library.R;
import hhm.android.library.datePicker.bean.DateParams;

public class DatePickDialog extends Dialog {

    private TextView cancelText;
    private TextView sureText;
    private DateTimePickerView mDateTimePickerView;
    private Builder mBuilder;

    private DatePickDialog(Context context, Builder builder) {
        super(context, R.style.dialog_style);
        mBuilder = builder;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cbk_dialog_pick_time);
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);

        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.width = outMetrics.widthPixels;
        getWindow().setAttributes(params);

        initView();
    }

    private void initView() {

        mDateTimePickerView = findViewById(R.id.wheelLayout);
        DateParams dateParams = new DateParams();
        dateParams.types = mBuilder.types;
        dateParams.currentDate = mBuilder.currentDate;
        dateParams.startDate = mBuilder.startDate;
        dateParams.endDate = mBuilder.endDate;
        mDateTimePickerView.show(dateParams, mBuilder.selectColor);

        cancelText = findViewById(R.id.cancel);
        cancelText.setOnClickListener(v -> dismiss());

        sureText = findViewById(R.id.sure);
        sureText.setOnClickListener(v -> {
            dismiss();
            if (mBuilder.onSureListener != null) {
                mBuilder.onSureListener.onSure(mDateTimePickerView.getSelectDate());
            }
        });
    }

    public static class Builder {
        private int[] types;
        private Date currentDate;
        private Date startDate;
        private Date endDate;
        private OnSureListener onSureListener;
        private int selectColor;

        public Builder setTypes(@DateParams.Type int[] types) {
            this.types = types;
            return this;
        }

        public Builder setCurrentDate(Date currentDate) {
            this.currentDate = currentDate;
            return this;
        }

        public Builder setStartDate(Date startDate) {
            this.startDate = startDate;
            return this;
        }

        public Builder setEndDate(Date endDate) {
            this.endDate = endDate;
            return this;
        }

        public Builder setTextColor(int selectColor) {
            this.selectColor = selectColor;
            return this;
        }

        public Builder setOnSureListener(OnSureListener onSureListener) {
            this.onSureListener = onSureListener;
            return this;
        }

        public void show(Context context) {
            DatePickDialog dialog = new DatePickDialog(context, this);
            dialog.show();
        }
    }
}
